import commands
import operator

def main():
	regex = 'malurl-20*'
	files = commands.getoutput('ls {0}'.format(regex)).split('\n')

	outf = open('malurl.stats', 'w')
	topoutf = open('top20.malurl.stats','w')
	label2count = dict()
	label2type = dict()
	for fname in files:
		print fname
		inf = open(fname, 'r')
		for line in inf:
			if line == '' or line == '\n':
				continue
			mytype = line.split('\',\'')[3]
			line = line.split('\',\'')[4]
			label2type[line] = mytype
			if line in label2count:
				label2count[line] = label2count[line] + 1
			else:
				label2count[line] = 1

	# for key, value in sorted(label2count.items()):
	count = 0
	for key, value in sorted(label2count.iteritems(), key=operator.itemgetter(1), reverse=True):
		outf.write(label2type[key] +  ',' + str(key) + ',' +  str(value) + '\n')
		if count < 20:
			topoutf.write(label2type[key] + ',' + str(key) + ',' + str(value) + '\n')
			count = count + 1
	outf.close()
	topoutf.close()
			


if __name__=="__main__":
        main()
